<?php
/**
 * 钉钉机器人定时任务
 */
namespace Portal\Controller;

use Common\Common\timingEvents;
use Common\Common\Tools;
use Common\Controller\HomebaseController;
date_default_timezone_set("Asia/Shanghai");

class TestController extends HomebaseController{
    
    // 页面内页
	public function index() {

		$send=array(
			'url'=>'https://oapi.dingtalk.com/robot/send?access_token=cf0bc78daa62e8d41dcaf7d723df4e33a0986995df5a842680112096154c0a4b',
			'data'=>array(
				'msgtype'=>'text',
				'text'=>array(
					'content'=>'定时发送日志测试11~',
				),
				'at'=>array(
					'atMobiles'=>array(
						'18200357750'
					),
					'isAtAll'=>true,
				),
			),
		);

		//timingEvents::addEvent('send_qfy_dd_log_message','http://www.qfy.com/Portal/Test/send',5,$send,true,10);
        timingEvents::addEvent('send_qfy_dd_log_message','http://www.tcioc.com/Portal/Test/sendLogMsgEveryDay',2,array(),true,60);

	}

    /**
     * 发送每日提交日志的提醒
     * @param string $validateTime
     * @param string $fridayTime
     * @return bool
     */
    public function sendLogMsgEveryDay($validateTime="17:45",$fridayTime="17:15"){

        // 判断当前时间，是否为 下午
        $currentTime=time();
        empty($validateTime) && $validateTime="17:45";
        $week=date("N",$currentTime);
        //$week=date("w--W--N",strtotime("2017-03-12 17:45"));
        //$currentDate=date("H:i",strtotime("17:45"));
        $currentDate=date("H:i",$currentTime);

        if($week>5){
            //周末，不用上班
            return false;
        }
        if($week==5){
            //星期五，下午5点半下班
            empty($fridayTime) && $fridayTime="17:15";
            $validateTime=$fridayTime;
        }

        if($currentDate!=$validateTime){
            //非指定时间无效
            echo "----Time Not Come----";
            return false;
        }

		$send=array(
			'url'=>'https://oapi.dingtalk.com/robot/send?access_token=cf0bc78daa62e8d41dcaf7d723df4e33a0986995df5a842680112096154c0a4b',
			'data'=>array(
				'msgtype'=>'text',
				'at'=>array(
					'atMobiles'=>array(
						//'18200357750'
					),
					'isAtAll'=>true,
				),
				'text'=>array(
					'content'=>'【'.$validateTime.'】各位，请提交今日的日志哈~',
				),
			),
		);

        $this->send($send['url'],$send['data']);

    }


    /**
     * 向钉钉发送消息
     * @param string $url 钉钉聊天机器人的地址
     * @param array $data 数组，参数  https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.2HLKqN&treeId=257&articleId=105735&docType=1
     */
	public function send($url,$data){
		$curl = curl_init();
		$this_header = array(
			"Content-type:application/json;Charset=UTF-8"
		);
		curl_setopt_array($curl,array(
			CURLOPT_URL=>$url,
			CURLOPT_HTTPHEADER=>$this_header,
			CURLOPT_POST=>TRUE,
			CURLOPT_POSTFIELDS=>json_encode($data),
			CURLOPT_RETURNTRANSFER=>1,
			CURLOPT_SSL_VERIFYPEER=>false,
			CURLOPT_SSL_VERIFYHOST=>false,
		));

		$result = curl_exec($curl);

		if (curl_errno($curl)) {
			echo 'Errno' . curl_error($curl);
		}
		curl_close($curl);

	}
	
}